﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.SharePoint;

using Ko2.Portal;

namespace EnvTest
{
    public partial class Form1 : Form
    {

        Env env = new Env();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            
            Exception ret = null;

            // Lost Accounts
            // ret = VerifyLostSPAccounts();

            // New Users
            ret = VerifyNewSPAccounts();

            // Найти активные учект SPUsers которых нет среди активных ADUsers
            // Найдены уволенные сотрудники

        }

        public Exception InsertErrSPUsers(DateTime DT, DataRow r)
        {
            Exception ret = null;
            Ko2.Portal.SP_List.SPList_errSPUsers oListHlp = new Ko2.Portal.SP_List.SPList_errSPUsers();
            string[] splitChar = { @"\" };
            string AccountName = "";
            string Login = "";
            string UserProfile_GUID = "";
            string ID1C = "";
            string HireDate = "";
            string ImageUrl = "";
            string SiteUrl = "";
            if (r["AccountName"] != DBNull.Value)
                AccountName = (string)r["AccountName"];
            if (!String.IsNullOrEmpty(AccountName))
            {
                string [] s = AccountName.Split(splitChar, StringSplitOptions.None);
                if(s.Length>=2)
                    Login = s[1];
            }
            if (r["ID"] != DBNull.Value)
                UserProfile_GUID = (string)r["ID"].ToString();
            if (r["ID1C"] != DBNull.Value)
                ID1C = (string)r["ID1C"];
            if (r["HireDate"] != DBNull.Value)
                HireDate = (string)r["HireDate"];
            if (r["ImageUrl"] != DBNull.Value)
                ImageUrl = (string)r["ImageUrl"];
            if (r["SiteUrl"] != DBNull.Value)
                SiteUrl = (string)r["SiteUrl"];
            oListHlp.Insert(DT, AccountName, Login, UserProfile_GUID, ID1C, HireDate, ImageUrl, SiteUrl);
            return ret;
        }

        private void appSettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string data = env.GetAppSetting("BIConnect");
        }

        private void verifyShopsNamesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();
            Ko2.DB.MySql mysql = new Ko2.DB.MySql();
            if (mysql.GetTradeOnline(ref tbl, DateTime.Now) == null)
            {
                env.VerifyShopsNames(ref tbl, "Магазин");
            }
        }

        private void webSettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            env.SetWebSetting_CreateInDoc_LastSuccessfully(DateTime.Now);
        }

        private void insertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_Log log = new Ko2.Portal.SP_Log();
            log.Insert("TheTest", "TestKey", "Test Sub Key", "Test Value");
        }

        private void birthdaysAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_BirthdaysAll o = new Ko2.Portal.SP_List.SPList_BirthdaysAll();

            DateTime dt = DateTime.Now;
        }

        private void sPUsersListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable tbl = null;
            List<string> errList = new List<string>();
            Ko2.Web.Utils.WS_Helper hlp =
                new Ko2.Web.Utils.WS_Helper();
            Exception ret = hlp.GetSPUsersList(ref tbl, ref errList);
            if (ret != null)
                MessageBox.Show("Error : " + ret.Message);
            else
                gv.DataSource = tbl;
        }

        public Exception VerifyLostSPAccounts()
        {
            Exception ret = null;
            DateTime DT = DateTime.Now;
            DT = new DateTime(DT.Year, DT.Month, DT.Day);
            List<string> errList = new List<string>();
            Ko2.Web.Utils.WS_Helper hlp = new Ko2.Web.Utils.WS_Helper();
            DataTable tblAD_allUsers = null;
            DataTable tblAD_allSPUsers = null;
            ret = hlp.GetADUsersListALL(ref tblAD_allUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }
            ret = hlp.GetSPUsersList(ref tblAD_allSPUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }

            // проверка 
            // не корректные SP учетки - ID1C == null - HireDate == null
            //
            Ko2.Portal.SP_List.SPList_errSPUsers oListHlp = new Ko2.Portal.SP_List.SPList_errSPUsers();
            oListHlp.DeleteAll();
            foreach (DataRow r in tblAD_allSPUsers.Rows)
            {
                if (r["ID1C"] == DBNull.Value)
                {
                    // save error
                    InsertErrSPUsers(DT, r);
                    continue;
                }
                else
                {
                    string ID1C = (string)r["ID1C"];
                    if (String.IsNullOrEmpty(ID1C))
                    {
                        // save error
                        InsertErrSPUsers(DT, r);
                        continue;
                    }
                }

                if (r["HireDate"] == DBNull.Value)
                {
                    // save error
                    InsertErrSPUsers(DT, r);
                    continue;
                }
                else
                {
                    string HireDate = (string)r["HireDate"].ToString();
                    if (String.IsNullOrEmpty(HireDate))
                    {
                        // save error
                        InsertErrSPUsers(DT, r);
                        continue;
                    }
                }
            }
            return ret;
        }

        public Exception VerifyNewSPAccounts()
        {
            Exception ret = null;
            DateTime DT = DateTime.Now;
            DT = new DateTime(DT.Year, DT.Month, DT.Day);
            DateTime DTStart = DT.AddDays(-7);
            DateTime DTEnd = DT.AddDays(2);
            List<string> errList = new List<string>();
            Ko2.Web.Utils.WS_Helper hlp = new Ko2.Web.Utils.WS_Helper();
            DataTable tbl_allSPUsers = null;
            ret = hlp.GetSPUsersList(ref tbl_allSPUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }

            Ko2.Portal.SP_List.SPList_NewUsers oListHlp = new Ko2.Portal.SP_List.SPList_NewUsers();
            oListHlp.DeleteAll();
            foreach (DataRow r in tbl_allSPUsers.Rows)
            {
                if (r["HireDate"] != DBNull.Value)
                {
                    DateTime HireDate = (DateTime)r["HireDate"];
                    if (HireDate >= DTStart && HireDate <= DTEnd)
                    {
                        DateTime HireDate2 = new DateTime(DTStart.Year, HireDate.Month, HireDate.Day);
                        string[] splitChar = { @"\" };
                        string AccountName = "";
                        string Login = "";
                        string UserProfile_GUID = "";
                        string ID1C = "";
                        string ImageUrl = "";
                        string SiteUrl = "";
                        bool IsOffice = false;
                        string OfficeName = "";
                        string DepartmentName = "";
                        string jobName = "";
                        string Phone = "";
                        string Mail = "";
                        if (r["AccountName"] != DBNull.Value)
                            AccountName = (string)r["AccountName"];
                        if (!String.IsNullOrEmpty(AccountName))
                        {
                            string [] s = AccountName.Split(splitChar, StringSplitOptions.None);
                            if(s.Length>=2)
                                Login = s[1];
                        }
                        if (r["ID"] != DBNull.Value)
                            UserProfile_GUID = (string)r["ID"].ToString();
                        if (r["ID1C"] != DBNull.Value)
                            ID1C = (string)r["ID1C"];
                        if (r["ImageUrl"] != DBNull.Value)
                            ImageUrl = (string)r["ImageUrl"];
                        if (r["SiteUrl"] != DBNull.Value)
                            SiteUrl = (string)r["SiteUrl"];
                        if (r["Office"] != DBNull.Value)
                        {
                            if ((string)r["Office"] == "Румянцево")
                                IsOffice = true;
                        }
                        if (r["Office"] != DBNull.Value)
                            OfficeName = (string)r["Office"];
                        if (r["Department"] != DBNull.Value)
                            DepartmentName = (string)r["Department"];
                        if (r["Staff"] != DBNull.Value)
                            jobName = (string)r["Staff"];
                        if (r["WorkPhone"] != DBNull.Value)
                            Phone = (string)r["WorkPhone"];
                        if (r["Mail"] != DBNull.Value)
                            Mail = (string)r["Mail"];
                        
                        Exception err =
                            oListHlp.Insert(HireDate2, IsOffice, OfficeName, DepartmentName, jobName, AccountName, Login, UserProfile_GUID, ID1C, HireDate.ToString(), ImageUrl, SiteUrl, Phone, Mail);
                        if (err != null)
                        {
                        }
                    }
                }
            }

            return ret;
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_BirthdaysAll o = new Ko2.Portal.SP_List.SPList_BirthdaysAll();
            DataTable tbl = new DataTable("tbl");
            List<string> errList = new List<string>();
            Exception ret = o.SelectFromWS(ref tbl, ref errList);
            if (ret == null)
            {
                gv.DataSource = tbl;
            }
        }

        private void newUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_NewUsers o = new Ko2.Portal.SP_List.SPList_NewUsers();
            DataTable tbl = new DataTable("tbl");
            List<string> errList = new List<string>();
            Exception ret = o.SelectFromWS(ref tbl, ref errList);
            if (ret == null)
            {
                gv.DataSource = tbl;
            }
        }

        private void permDocsSelectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            DateTime dt = DateTime.Now;
            dt = new DateTime(dt.Year, dt.Month, dt.Day);
            DataTable tbl = new DataTable("tbl");
            List<string> errList = new List<string>();
            Exception ret = o.Select(ref tbl, dt);
            if (ret == null)
            {
                gv.DataSource = tbl;
            }
        }

        private void permDocsSelectAllToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            DateTime dt = DateTime.Now;
            //dt = new DateTime(dt.Year, dt.Month, dt.Day, 8,10,1);
            dt = new DateTime(dt.Year, dt.Month, dt.Day, 8, 10, 1);
            DataTable tbl = new DataTable("tbl");
            List<string> errList = new List<string>();
            Exception ret = o.SelectGeq(ref tbl, dt);
            if (ret == null)
            {
                gv.DataSource = tbl;
            }
        }

        private void permDocsGetItemByIDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            DataTable tbl = null;
            List<string> errList = new List<string>();
            Exception ret = o.GetFilesListByItemId(ref tbl, 353962);
            if (ret == null && tbl != null)
            {
                gv.DataSource = tbl;
            }
        }

        private void getSPUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable("tbl");
            Exception err = new Ko2.Portal.Users.SPUsersHelper().GetUsers(ref tbl, @"http://s-moss01.korablik.local/");
        }

        private void permDocGetFileDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            byte[] data = null;
            Exception ret = o.GetFileData(ref data, 384940, @"123.jpg");
        }

        private void permDocApproveItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            o.ApproveOrRegectItem(386483, Ko2.Portal.SP_List.SPList_PermDocs.DocStatus.Ожидают_утверждения);
        }

        private void permDocSelectForApproveListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();
            Ko2.Portal.SP_List.SPList_PermDocs o = new Ko2.Portal.SP_List.SPList_PermDocs();
            Exception err = o.SelectForApproveList(ref tbl);
        }

    }
}
